#include "../include/head.h"
//录入信息
int input( ElemType *data,p_list head)
{
   
    printf("请输入学生信息(姓名 年龄 学号):");
    scanf("%s %d %d", data->name, &data->age, &data->id);
    if (insert(head, *data) == -1)//讲信息插入链表
    {

        return -1;
    }
    return 0;
}
//将录入的信息写入文件
int write_file(p_list head)
{
    FILE *fp = fopen(STUDENTS_FILE, "w");
    if (fp == NULL)
    {
        printf("打开文件失败\n");
        return -1;
    }
    p_list p = head->next;
    while (p != NULL)
    {
        fprintf(fp, "%s %d %d\n", p->data.name, p->data.age, p->data.id);//可以用fwrite
        p = p->next;

    }
    fclose(fp);
}
//从文件中将数据读取出来
int read_file(p_list head)
{
    FILE *fp = fopen(STUDENTS_FILE, "r");
    if (fp == NULL)
    {
        printf("打开文件失败\n");
        return -1;
    }
    ElemType data;
    while (fscanf(fp, "%s %d %d", data.name, &data.age, &data.id) != EOF)//每读一次 插入一次 可以fread
    {
        insert(head, data);
    }
    fclose(fp);
    return 0;
}
